﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;

namespace TestDxfDocument
{
    class fang
    {
        //hp1为放置点
        //length1为第一条指引线长度
        //angle1为第一条线角度
        //length2为第二指引条线长度
        //angle2为第二条线角度
        //weighth为框格宽度
        //arrowscale为箭头大小
        //scale为框的尺寸比例
        //text1为从右往左数第一个框内字符
        //text2为中间框字符
        //height为框内字符高
        //fh为公差符号
        //fhscale为公差符号尺寸
        public static void gcbz(DxfDocument dxf, Vector2 hp1, double lenghth1, double angle1, double lenghth2, double angle2, double weighth, double arrowscale, double scale, string text1, string text2, double height, string fh, double fhscale)
        {
            double pi = Math.PI;
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (14 + angle1) * pi / 180, 2.05 * arrowscale);
            Vector2 a3 = fun.Polar(a1, (angle1 - 14) * pi / 180, 2.05 * arrowscale);
            Vector2 a4 = fun.Polar(a1, angle1 * pi / 180, 1.5 * arrowscale);
            Vector2 a5 = fun.Polar(a4, angle1 * pi / 180, lenghth1);
            Vector2 a6 = fun.Polar(a5, angle2 * pi / 180, lenghth2);

            //框
            Vector2 a7 = fun.Polar(a6, pi / 2, 2.5 * scale);
            Vector2 a8 = fun.mirrorpoint(a7, a5, a6);
            Vector2 a7_1_1 = fun.Polar(a7, pi, weighth);
            Vector2 a7_1_2 = fun.Polar(a7_1_1, pi, weighth * 1.5);
            Vector2 a7_1 = fun.Polar(a7_1_2, pi, weighth);
            Vector2 a8_1_1 = fun.Polar(a8, pi, weighth);
            Vector2 a8_1_2 = fun.Polar(a8_1_1, pi, weighth * 1.5);
            Vector2 a8_1 = fun.Polar(a8_1_2, pi, weighth);

            Vector2 zi = fun.Inters(a7, a8_1_1, a7_1_1, a8);
            Vector2 zi2 = fun.Inters(a7_1_2, a8_1_1, a7_1_1, a8_1_2);

            Line line1 = new Line(a4, a5);
            Line line2 = new Line(a5, a6);
            Line line3 = new Line(a6, a7);
            Line line4 = new Line(a8, a7);
            Line line5 = new Line(a7, a7_1);
            Line line6 = new Line(a8_1, a7_1);
            Line line7 = new Line(a8_1, a8);
            Line line8 = new Line(a8_1_1, a7_1_1);
            Line line9 = new Line(a8_1_2, a7_1_2);

            Solid solid = new Solid(a1, a2, a3);

            double ziwidth = text1.Length;
            Text text = new Text(text1, new Vector2(zi.X - ziwidth / 1.4, zi.Y - height / 2.0), height, 0);
            double ziwidth2 = text2.Length;
            Text text_1 = new Text(text2, new Vector2(zi2.X - ziwidth2 / 1.4, zi2.Y - height / 2.0), height, 0);

            fang.xwgcfh1(dxf, new Vector2(a8_1.X + fun.CalDistance(a8_1, a8_1_2) / 2, a8_1.Y + fun.CalDistance(a7, a8) / 2), fh, fhscale);

            dxf.AddEntity(new EntityObject[] { solid, line1, line2, line3, line4, line5, line6, line7, line8, line9, text, text_1 });
        }

        public static void xwgcfh1(DxfDocument dxf1, Vector2 hp1, String zh, double scale)  //形位公差符号
        {
            double pi = 3.1415926535897;
            if (zh == "zxd")
            {
                Vector2 v0 = new Vector2(hp1.X, hp1.Y);
                Vector2 v1 = fun.Polar(v0, pi, 2.5 * scale);
                Vector2 v2 = fun.Polar(v0, 0, 2.5 * scale);
                Line line = new Line(v1, v2);
                dxf1.AddEntity(new EntityObject[] { line });
            }
            if (zh == "pmd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5 * scale, hp1.Y + 2 * scale);
                Vector2 hp3 = new Vector2(hp1.X + 3 * scale, hp1.Y + 2 * scale);
                Vector2 hp4 = new Vector2(hp1.X + 1.5 * scale, hp1.Y - 2 * scale);
                Vector2 hp5 = new Vector2(hp1.X - 3 * scale, hp1.Y - 2 * scale);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp3, hp4);
                Line line3 = new Line(hp4, hp5);
                Line line4 = new Line(hp5, hp2);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3, line4 });
            }
            if (zh == "jd")
            {
                Vector2 v1 = new Vector2(hp1.X, hp1.Y);
                Vector2 v2 = fun.Polar(v1, -142 * pi / 180, 3.51 * scale);
                Vector2 v3 = fun.Polar(v2, 45, 5.9 * scale);
                Vector2 v4 = fun.Polar(v2, 0, 4.65 * scale);



                Line line1 = new Line(v2, v3);
                Line line2 = new Line(v2, v4);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "yd")
            {
                Circle cir = new Circle(hp1, 2.5 * scale);
                dxf1.AddEntity(new EntityObject[] { cir });
            }
            if (zh == "yzd")
            {
                Vector2 v0 = new Vector2(hp1.X, hp1.Y);
                Vector2 v1 = fun.Polar(v0, 160 * pi / 180, 1.5 * scale);
                Vector2 v2 = fun.Polar(v1, -110 * pi / 180, 2.3 * scale);
                Vector2 v3 = fun.Polar(v1, 70 * pi / 180, 1.8 * scale);
                Vector2 v4 = fun.Polar(v0, -20 * pi / 180, 1.5 * scale);
                Vector2 v5 = fun.Polar(v4, -110 * pi / 180, 1.8 * scale);
                Vector2 v6 = fun.Polar(v4, 70 * pi / 180, 2.3 * scale);


                Line line1 = new Line(v2, v3);
                Line line2 = new Line(v6, v5);
                Circle cir = new Circle(v0, 1.5);
                dxf1.AddEntity(new EntityObject[] { line1, line2, cir });
            }
            if (zh == "xlkd")
            {
                Arc arc = new Arc(new Vector2(hp1.X, hp1.Y - 1.5 * scale), 3 * scale, 0, 180);
                dxf1.AddEntity(new EntityObject[] { arc });
            }
            if (zh == "mlkd")
            {
                Arc arc = new Arc(new Vector2(hp1.X + 0.72, hp1.Y - 1.5 * scale), 3 * scale, 0, 180);
                Line line1 = new Line(new Vector2(hp1.X - 3 * scale + 0.72 * scale, hp1.Y - 1.5 * scale), new Vector2(hp1.X + 3 * scale + 0.72 * scale, hp1.Y - 1.5 * scale));
                dxf1.AddEntity(new EntityObject[] { arc, line1 });
            }
            if (zh == "pxd")
            {
                double ll = 3 / Math.Cos(45 * pi / 180);
                Vector2 hpp = new Vector2(hp1.X - ll * 0.5 * scale * Math.Cos(45 * pi / 180), hp1.Y - ll * 0.5 * scale * Math.Sin(45 * pi / 180));
                Vector2 hppp = new Vector2(hp1.X + ll * 0.5 * scale * Math.Cos(45 * pi / 180), hp1.Y + ll * 0.5 * scale * Math.Sin(45 * pi / 180));
                Vector2 hp2 = new Vector2(hpp.X - 0.2 * scale * ll, hpp.Y);
                Vector2 hp3 = new Vector2(hppp.X - 0.2 * scale * ll, hppp.Y);
                Vector2 hp4 = new Vector2(hpp.X + 0.2 * scale * ll, hpp.Y);
                Vector2 hp5 = new Vector2(hppp.X + 0.2 * scale * ll, hppp.Y);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "czd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 2 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp3 = new Vector2(hp1.X + 2 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp4 = new Vector2(hp1.X, hp1.Y + 2.5 * scale);
                Vector2 hp5 = new Vector2(hp1.X, hp1.Y - 1.5 * scale);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "qxd")
            {
                Vector2 hp2 = new Vector2(hp1.X + 3 * scale, hp1.Y + 1.5 * scale);
                Vector2 hp3 = new Vector2(hp1.X - 3 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp4 = new Vector2(hp1.X + 3 * scale, hp1.Y - 1.5 * scale);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp3, hp4);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "tzd")
            {
                Circle cir1 = new Circle(hp1, 1.0 * scale);
                Circle cir2 = new Circle(hp1, 1.5 * scale);

                dxf1.AddEntity(new EntityObject[] { cir1, cir2 });
            }
            if (zh == "dcd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 3 * scale, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 3 * scale, hp1.Y);
                Vector2 hp4 = new Vector2(hp1.X - 1.5 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp5 = new Vector2(hp1.X + 1.5 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp6 = new Vector2(hp1.X - 1.5 * scale, hp1.Y + 1.5 * scale);
                Vector2 hp7 = new Vector2(hp1.X + 1.5 * scale, hp1.Y + 1.5 * scale);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                Line line3 = new Line(hp6, hp7);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3 });
            }
            if (zh == "wzd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 2 * scale, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 2 * scale, hp1.Y);
                Vector2 hp4 = new Vector2(hp1.X, hp1.Y + 2 * scale);
                Vector2 hp5 = new Vector2(hp1.X, hp1.Y - 2 * scale);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                Circle cir = new Circle(hp1, 1.2 * scale);
                dxf1.AddEntity(new EntityObject[] { line1, line2, cir });
            }
            if (zh == "ytd")
            {
                Vector2 hp2 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5 * scale);
                Vector2 hp3 = new Vector2(hp1.X - 1.5, hp1.Y - 1.5 * scale);
                Vector2 hp4 = new Vector2(hp2.X - 2.5 * scale * Math.Sin(55 * pi / 180), hp2.Y - 2.5 * scale * Math.Cos(55 * pi / 180));
                Vector2 hp5 = new Vector2(hp2.X - 2.5 * scale * Math.Sin(35 * pi / 180), hp2.Y - 2.5 * scale * Math.Cos(35 * pi / 180));
                Line line1 = new Line(hp2, hp3);
                Solid sd = new Solid(hp4, hp5, hp2);
                dxf1.AddEntity(new EntityObject[] { line1, sd });
            }
            if (zh == "qtd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp3 = new Vector2(hp2.X - 1.5 * scale, hp2.Y);
                Vector2 hp4 = new Vector2(hp2.X + 1.5 * scale, hp2.Y);
                Vector2 hp5 = new Vector2(hp3.X + 3 * scale, hp3.Y + 3);
                Vector2 hp6 = new Vector2(hp4.X + 3 * scale, hp4.Y + 3);
                Vector2 hp7 = new Vector2(hp5.X - 2.5 * scale * Math.Sin(55 * pi / 180), hp5.Y - 2.5 * scale * Math.Cos(55 * pi / 180));
                Vector2 hp8 = new Vector2(hp5.X - 2.5 * scale * Math.Sin(35 * pi / 180), hp5.Y - 2.5 * scale * Math.Cos(35 * pi / 180));
                Vector2 hp9 = new Vector2(hp6.X - 2.5 * scale * Math.Sin(55 * pi / 180), hp6.Y - 2.5 * scale * Math.Cos(55 * pi / 180));
                Vector2 hp10 = new Vector2(hp6.X - 2.5 * scale * Math.Sin(35 * pi / 180), hp6.Y - 2.5 * scale * Math.Cos(35 * pi / 180));
                Line line1 = new Line(hp5, hp3);
                Line line2 = new Line(hp3, hp4);
                Line line3 = new Line(hp4, hp6);
                Solid sd1 = new Solid(hp7, hp8, hp5);
                Solid sd2 = new Solid(hp9, hp10, hp6);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3, sd1, sd2 });
            }
            if (zh == "zdstzt")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.0 * scale, hp1.Y - 1.5 * scale);
                Circle cir = new Circle(hp1, 3 * scale);
                Text text = new Text("M", hp2, 3.5 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }
            if (zh == "zxys")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.0 * scale, hp1.Y - 1.5 * scale);
                Circle cir = new Circle(hp1, 3 * scale);
                Text text = new Text("A", hp2, 3.5 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }
            if (zh == "ysgcd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 0.5 * scale, hp1.Y - 1.5 * scale);
                Circle cir = new Circle(hp1, 3 * scale);
                Text text = new Text("P", hp2, 3.5 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }

            if (zh == "bryz")
            {
                Vector2 hp2 = new Vector2(hp1.X - 0.5 * scale, hp1.Y - 1.5 * scale);
                Circle cir = new Circle(hp1, 3 * scale);
                Text text = new Text("E", hp2, 3.5 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }
            if (zh == "yx")
            {
                Text text = new Text("(-)", new Vector2(hp1.X - 3.5 * scale, hp1.Y - 1.5 * scale), 3 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { text });
            }
            if (zh == "yxzt")
            {
                Text text = new Text("(+)", new Vector2(hp1.X - 3.5 * scale, hp1.Y - 1.5 * scale), 3 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { text });
            }
            if (zh == "yxjx")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5);
                Vector2 hp4 = new Vector2(hp1.X + 1.5, hp1.Y - 1.5);
                Vector2 hp5 = new Vector2(hp1.X - 2.5, hp1.Y - 1.5);
                Vector2 hp6 = new Vector2(hp1.X + 2.5, hp1.Y - 1.5);

                Text text1 = new Text("(", hp5, 3.5, 0);
                text1.WidthFactor = 0.7;
                Text text2 = new Text(")", hp6, 3.5, 0);
                text2.WidthFactor = 0.7;
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp2, hp4);
                Line line3 = new Line(hp3, hp4);
                dxf1.AddEntity(new EntityObject[] { text1, text2, line1, line2, line3 });
            }
            if (zh == "jzmbwd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y + 1.5);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y - 1.5);
                Vector2 hp4 = new Vector2(hp1.X - 1.5, hp1.Y - 1.5);
                Vector2 hp5 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5);

                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "jt")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5 * scale, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 1.5 * scale, hp1.Y);
                Vector2 hp4 = new Vector2(hp3.X - 2.5 * scale, hp3.Y + 2.5 * scale * Math.Tan(10 * pi / 180));
                Vector2 hp5 = new Vector2(hp3.X - 2.5 * scale, hp3.Y - 2.5 * scale * Math.Tan(10 * pi / 180));

                Line line1 = new Line(hp2, hp3);
                Solid sd = new Solid(hp4, hp5, hp3);
                dxf1.AddEntity(new EntityObject[] { line1, sd });
            }
            if (zh == "jzx")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y);

                Line line1 = new Line(hp2, hp3);
                dxf1.AddEntity(new EntityObject[] { line1 });
            }
            if (zh == "bcfwwzfx")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y + 1.5);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5);
                Vector2 hp4 = new Vector2(hp1.X + 1.5, hp1.Y - 1.5);
                Vector2 hp5 = new Vector2(hp1.X - 1.5, hp1.Y - 1.5);

                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp3, hp4);
                Line line3 = new Line(hp4, hp5);
                Line line4 = new Line(hp5, hp2);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3, line4 });
            }

        }
    }
}
